﻿using Assets.Editor.BehaviorEditor.Controller;
using Assets.Editor.BehaviorEditor.View;
using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

namespace Assets.Editor.BehaviorEditor
{
    public class BehaviorEditorWindow : EditorWindow, IRectControl
    {
        private NodeEditorView nodeEditorView;

        private BehaviorEditorWindowController behaviorEditorWindowController;

        private bool isCheckAddBtn;

        public BehaviorEditorWindow()
        {
            nodeEditorView = new NodeEditorView(this);
            nodeEditorView.ExitAddNodeStatus += NodeEditorView_ExitAddNodeStatus;

            behaviorEditorWindowController = new BehaviorEditorWindowController();
        }

        private void NodeEditorView_ExitAddNodeStatus()
        {
            isCheckAddBtn = false;
        }

        private void OnGUI()
        {
            Event e = Event.current;

            Draw();
            ProccessEvent(e);

            if(GUI.changed)
            {
                Repaint();
            }
        }

        private void Draw()
        {
            GUILayout.BeginHorizontal();

            GUI.enabled = !isCheckAddBtn;
            if(GUILayout.Button("Add Node A"))
            {
                isCheckAddBtn = true;
                nodeEditorView.OnCheckedNodeMenuBtn("Node A");
            }
            GUI.enabled = true;

            GUILayout.EndHorizontal();

            nodeEditorView.Draw();
        }

        private void ProccessEvent(Event e)
        {
            nodeEditorView.ProccessEvent(e);
        }

        [MenuItem("MyTools/BehaviorEditor")]
        public static void Open()
        {
            GetWindow(typeof(BehaviorEditorWindow));
        }

        public Rect GetPos()
        {
            return position;
        }
    }
}